package zcrypto

import (
	"crypto/md5"
	"crypto/sha1"
	"encoding/base64"
	"encoding/hex"
	"fmt"
	"io"
	"strings"
)

func Md5(str string) string {
	h := md5.New()
	h.Write([]byte(str))
	s := h.Sum(nil)
	return strings.ToUpper(hex.EncodeToString(s))
}

func Sha1(str string) (string, error) {
	t := sha1.New()
	_, e := io.WriteString(t, str)
	return fmt.Sprintf("%x", t.Sum(nil)), e
}

func Md5Base64Str(str string) string {
	h := md5.New()
	h.Write([]byte(str))
	s := h.Sum(nil)
	return EncryptBase64(s)
}

// EncryptBase64 base64转码
func EncryptBase64(source []byte) string {
	return base64.StdEncoding.EncodeToString(source)
}

// DecodeBase64 base64解码
func DecodeBase64(base64Str string) ([]byte, error) {
	return base64.StdEncoding.DecodeString(base64Str)

}
